package com.bookseek.xreader.service;

import android.util.Log;

import com.bookseek.xreader.consts.XreaderConts;
import com.bookseek.xreader.utils.JkhlUtils;
import com.bookseek.xreader.utils.MiniDataUtils;
import com.bookseek.xreader.utils.OkHttpUtils;
import com.bookseek.xreader.vo.MiniBaseInfoVo;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import okhttp3.Call;
import okhttp3.Response;

public class DataService {

    private static DataService instance;

    private static MiniBaseInfoVo CFG;


    private DataService(){}

    public static DataService getInstance(){
        if(instance == null){
            synchronized (DataService.class){
                if(instance == null){
                    instance =  new DataService();
                }
            }
        }
        return instance;
    }

    public void initAppCfgCall(String api, Consumer<MiniBaseInfoVo> consumer){
        Log.i(XreaderConts.TAG,"api="+api);
        OkHttpUtils.getInstance().get(api, new OkHttpUtils.SimpleCallback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(XreaderConts.TAG,response.isSuccessful()+":"+response.code());
                if(response.isSuccessful()){
                    String text = response.body().string();
                    String jsonText = MiniDataUtils.aesDecryptCBCWithIvFromBase64SafeUrlJSONP(text);
                    MiniBaseInfoVo cfgInfo = OkHttpUtils.gson.fromJson(jsonText,MiniBaseInfoVo.class);
                    Log.i(XreaderConts.TAG,"cfgInfo="+OkHttpUtils.gson.toJson(cfgInfo));
                    if(consumer != null){
                        consumer.accept(cfgInfo);
                    }
                    CFG = cfgInfo;
                }
            }
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                if(consumer != null){
                    consumer.accept(null);
                }
                super.onFailure(call, e);
            }
        });
    }

    public void initAppCfg(Consumer<MiniBaseInfoVo> consumer){
        String ynoteApi = String.format("https://note.youdao.com/yws/api/note/fa80895f82421f3b7f01980673c960d3?sev=j1&editorType=1&unloginId=%s&editorVersion=new-json-editor&sec=v1",JkhlUtils.uuid());
        //plat ynote
        initAppCfgCall(ynoteApi,ret->{
            if(ret != null){
                Log.i(XreaderConts.TAG,"ynote cfg is ok");
                if(consumer != null){
                    consumer.accept(ret);
                }

            }else{
                String bookseekApi = "https://bookseek.top/taobaog?bcode=bbbinfo";
                initAppCfgCall(bookseekApi,seekRet->{
                    if(seekRet != null){
                        if(consumer != null){
                            consumer.accept(seekRet);
                        }
                    }else{
                        Log.i(XreaderConts.TAG,"无法获取cfg");
                    }
                });
            }
        });
    }





}
